package com.lz.football.module.fameHall
{
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.events.FameHallEvent;
	import com.liangzhong.events.ModuleEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.FameHall.vo.FameHallVo;
	import com.liangzhong.model.goods.vo.GoodsVo;
	import com.liangzhong.model.goods.vo.PlayerStarVo;
	import com.lz.football.module.fameHall.item.FameHallItem;
	import com.lz.football.module.playerCenter.PlayerCard;
	
	import common.BaseView.BaseView;
	import common.BaseView.GoodItem;
	import common.base.GoodsUsePanel;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import lz.controls.Alert;
	import lz.controls.LButton;
	import lz.controls.ScrollPolicy;
	import lz.controls.containers.ScrollPane;
	import lz.controls.managers.PopUpManager;
	
	/**
	 *荣誉堂 
	 * @author Administrator
	 * 
	 */	
	public class FameHallView extends BaseView implements IFameHall
	{
		
		/**
		 *滚动条 
		 */
		private var _scroll:ScrollPane;
		/**
		 *滚动条容器 
		 */
		private var _scrollContain:Sprite;
		
		/**
		 *球员1 
		 */
		private var card_1:MovieClip;
		/**
		 *球员2 
		 */
		private var card_2:MovieClip;
		/**
		 *球员3
		 */
		private var card_3:MovieClip;
		
		/**
		 *按钮1 
		 */
		private var btn_1:LButton;
		/**
		 *按钮2
		 */
		private var btn_2:LButton;
		/**
		 *按钮3
		 */
		private var btn_3:LButton;
		
		/**
		 *选中的item 
		 */
		private var _selectItem:FameHallItem;
		/**
		 *选中的vo 
		 */
		private var _selectVo:FameHallVo;
		
		
		/**
		 *选择的球员 vo
		 */
		private var _selectStarVo:GoodsVo;
		
		/**
		 *选择的旋转的位置位置 
		 */
		private var _selectType:int;
		
		/**
		 *奖励描述 
		 */
		private var awardDes:TextField;
		
		/**
		 *奖励物品 
		 */
		private var awardItem:GoodItem;
		
		
		/**
		 *球员卡 1
		 */
		private var _card1:PlayerCard;
		/**
		 *球员卡 2
		 */
		private var _card2:PlayerCard;
		/**
		 *球员卡 3
		 */
		private var _card3:PlayerCard;
		
		/**
		 *奖励物品容器 
		 */
		private var awardContain:MovieClip;
		
		/**
		 *领取状态  
		 */
		private var finishStatus:MovieClip;
		/**
		 *领取奖励按钮 
		 */
		private var getBtn:SimpleButton;
		
		public function FameHallView()
		{
			//TODO: implement function
			super();
		}
		override public function initUI(view:Object=null):void
		{
			symbolSkin = Reflection.createInstance("com.lz.modul.famehall.famehallView");
			this.addChild(symbolSkin);
			_windowBg = symbolSkin.hameBg;
			_viewHeight = symbolSkin.height;
			_viewWidth = symbolSkin.width;
			awardContain = symbolSkin.awardContain
			isCloseBtn = true;
			isDrag = true;
			moveClose(_viewWidth - 35,40);
			card_1 = symbolSkin.card_1;
			card_2 = symbolSkin.card_2;
			card_3 = symbolSkin.card_3;
			card_1.gotoAndStop(5);
			card_2.gotoAndStop(5);
			card_3.gotoAndStop(5);
			_card1 = new PlayerCard();
			_card1.initUI();
			card_1.mc.mc.addChild(_card1);
			
			_card2 = new PlayerCard();
			_card2.initUI();
			card_2.mc.mc.addChild(_card2);
			
			_card3 = new PlayerCard();
			_card3.initUI();
			card_3.mc.mc.addChild(_card3);
			
			
			
			card_1.visible = card_2.visible = card_3.visible = false;
			btn_1 = new LButton(symbolSkin.btn_1);
			btn_1.enable = true;
			btn_2 = new LButton(symbolSkin.btn_2);
			btn_2.enable = true;
			btn_3 = new LButton(symbolSkin.btn_3);
			awardDes = symbolSkin.awardDes;
			
			finishStatus = symbolSkin.finishStatus;
			getBtn = symbolSkin.getBtn;
			getBtn.visible = false;
			finishStatus.gotoAndStop(3);
			btn_3.enable = true;
			resetStatuc();
			awardItem = new GoodItem();
			awardItem.initUI();
			awardContain.addChild(awardItem);
			onResizeHandler();
			initScroll();
			setItem();
			addEvtListener();
		}
		/**
		 *初始化流动条 
		 * 
		 */
		private function initScroll():void
		{
			_scrollContain = new Sprite();
			_scroll = new ScrollPane();
			_scroll.horizontalScrollPolicy = ScrollPolicy.OFF;
			_scroll.setSize(163,383);
			this.addChild(_scroll);
			_scroll.x = 16;
			_scroll.y = 93;
			_scroll.update();
			
		}
		private function setItem():void
		{
			var arr:Array = AppData.getInstance().fameHallDateManager.fameHallList;
			var len:int = arr.length;
			
			var item:FameHallItem;
			for(var i:int = 0;i < len;i++)
			{
				item = new FameHallItem();
				item.initUI();
				item.setDate(arr[i]);
				item.x = 2;
				item.callBack = selectItem;
				item.y = (50) * i;
				_scrollContain.addChild(item);
				if(i == 0)
					selectItem(item);
			}
			_scroll.source = _scrollContain;
			_scroll.update();
			
		}
		
		/**
		 *选中了item 
		 * @param $item
		 * 
		 */
		private function selectItem($item:FameHallItem):void
		{
			if(_selectItem != null)_selectItem.select = false;
			_selectItem = $item;
			_selectItem.select = true;
			resetStatuc();
			showInfo($item.data as FameHallVo);
			
		}
		/**
		 *显示选中的荣誉信息 
		 * @param $vo
		 * 
		 */		
		private function showInfo($vo:FameHallVo):void
		{
			_selectVo = $vo;
			var starVo:GoodsVo = AppData.getInstance().goodsManager.getStarById(_selectVo.palyerid_1);
			if(_selectVo.palyerid_1 == _selectVo.starid1)//已经开启
			{
				card_1.visible = true;
				_card1.setStartDate(starVo as PlayerStarVo);
				card_1.gotoAndStop(5);
			}else
			{
				btn_1.visible = true;
				symbolSkin.btn_1.playerName.text = "" + starVo.name;
			}
			
			
			
			starVo = AppData.getInstance().goodsManager.getStarById(_selectVo.palyerid_2);
			if(_selectVo.palyerid_2 == _selectVo.starid2)//已经开启
			{
				card_2.visible = true;
				_card2.setStartDate(starVo as PlayerStarVo);
				card_2.gotoAndStop(5);
			}else
			{
				btn_2.visible = true;
				symbolSkin.btn_2.playerName.text = "" + starVo.name;
			}
			
			
			
			
			starVo = AppData.getInstance().goodsManager.getStarById(_selectVo.palyerid_3);
			if(_selectVo.palyerid_3 == _selectVo.starid3)//已经开启
			{
				card_3.visible = true;
				_card3.setStartDate(starVo as PlayerStarVo);
				card_3.gotoAndStop(5);
			}else
			{
				btn_3.visible = true;
				symbolSkin.btn_3.playerName.text = "" + starVo.name;
			}
			
			if(_selectVo.active == 1)
			{
				awardDes.text = "已激活：" + _selectVo.descplay;
				awardDes.textColor = 0x00FF00;
			}else
			{
				awardDes.textColor = 0x999999;
				awardDes.text = "未激活：" +  _selectVo.descplay;
			}
			awardItem.data = AppData.getInstance().goodsManager.getGoodsByTypeAndId(_selectVo.ntype,_selectVo.id);
			
			switch(_selectVo.status)
			{
				case 0:
					finishStatus.gotoAndStop(1);
					break;
				case 1:
					getBtn.visible = true;
					getBtn.addEventListener(MouseEvent.CLICK,onGetHandler);
					break;
				case 2:
					finishStatus.gotoAndStop(2);
					break;
			}
			if(GoodsUsePanel.inistance.parent)
				showPlayerPanel();
		}
		
		/**
		 *选择球员 
		 * @param $goodsVo
		 * 
		 */		
		private function selectPalyer($goodsVo:GoodsVo):void
		{
			if($goodsVo != null)
			{
				_selectStarVo = $goodsVo;
				Alert.show("将球员放入荣耀堂后仓库内球员道具将会消失，确定放入？","",Alert.NO,GlobalDef.AlertContainer,onAlertPlayer);
			}
		}
		/**
		 *确认是否放置球员 
		 * @param $type
		 * 
		 */		
		private function onAlertPlayer($type:int):void
		{
			if($type == Alert.YES)
			{
				if(GoodsUsePanel.inistance.parent)
					GoodsUsePanel.inistance.parent.removeChild(GoodsUsePanel.inistance);
				this.dispatchEvent(new FameHallEvent(FameHallEvent.PUTPLAYER,{honourid:_selectVo.honourid,cellid:_selectStarVo.pid,ntype:_selectType}));
			}
		}
		
		
		/**
		 *重置状态 按钮和球员卡 
		 * 
		 */
		private function resetStatuc():void
		{
			card_1.visible = card_2.visible = card_3.visible = false;
			btn_1.visible = btn_2.visible = btn_3.visible = false;
			getBtn.visible = false;
			finishStatus.gotoAndStop(3);
		}
		/**
		 *关闭 
		 * @param event
		 * 
		 */		
		override public function onCloseHandler(event:MouseEvent):void
		{
			this.dispatchEvent(new ModuleEvent(ModuleEvent.REMOVE_MODULE));
		}
		override public function addEvtListener():void
		{
			btn_1.addEventListener(MouseEvent.CLICK,onClickBtnHandler);
			btn_2.addEventListener(MouseEvent.CLICK,onClickBtnHandler);
			btn_3.addEventListener(MouseEvent.CLICK,onClickBtnHandler);
		}
		/**
		 *点击按钮 
		 * @param event
		 * 
		 */		
		protected function onClickBtnHandler(event:Event):void
		{
			// TODO Auto-generated method stub
			
			switch(event.currentTarget)
			{
				case btn_1:
					_selectType = 1;
					
					break;
				case btn_2:
					
					_selectType = 2
					break;
				case btn_3:
					
					_selectType = 3;
					break;
			}
		
			showPlayerPanel();
		}
		
		/**
		 *显示背包中的球员  
		 * 
		 */
		private function showPlayerPanel():void
		{
			var arr:Array;
			switch(_selectType)
			{
				case 1:
					arr = AppData.getInstance().bagDataManager.getStarsGoodsByStarId(_selectVo.palyerid_1);
					break;
				case 2:
					arr = AppData.getInstance().bagDataManager.getStarsGoodsByStarId(_selectVo.palyerid_2);
					break;
				case 3:
					arr = AppData.getInstance().bagDataManager.getStarsGoodsByStarId(_selectVo.palyerid_3);
					break;
				default:
					arr = AppData.getInstance().bagDataManager.getStarsGoodsByStarId(_selectVo.palyerid_1);
					break;
			}
//			GoodsUsePanel.inistance.initPlace();
//			GoodsUsePanel.inistance.showBtn(GoodsUsePanel.BTN_TYPE_5);
//			GoodsUsePanel.inistance.goodsList = arr;
//			GoodsUsePanel.inistance.callBack = selectPalyer;
			
			
			var goodUsepanel:GoodsUsePanel = GoodsUsePanel.inistance;
			goodUsepanel.dragEnabled = true;
			goodUsepanel.addEvtListener();
			goodUsepanel.showBtn(GoodsUsePanel.BTN_TYPE_5);
			goodUsepanel.goodsList = arr;
			goodUsepanel.callBack = selectPalyer;
			PopUpManager.addWindow(goodUsepanel,GlobalDef.GameWindowContainer,(GlobalDef.StageWidth - 380) >> 1,(GlobalDef.StageHeight - 315)>> 1,true);
		}
		
		/**
		 *放置球员返回  honourid  cellid  ntype
		 * @return 
		 * 
		 */
		public function getPutPlayer($obj:Object):void
		{
			if($obj == null || $obj.honourid != _selectVo.honourid)return;
			
			if(GoodsUsePanel.inistance.parent)
				GoodsUsePanel.inistance.parent.removeChild(GoodsUsePanel.inistance);
			var starVo:GoodsVo;
			starVo = AppData.getInstance().goodsManager.getStarById(_selectVo["palyerid_"+ $obj.ntype]);
			this["card_"+ $obj.ntype].visible = true;
			this["_card"+ $obj.ntype].setStartDate(starVo as PlayerStarVo);
			this["card_"+ $obj.ntype].gotoAndPlay(1);
			if(_selectVo.active == 1)
			{
				getBtn.visible = true;
				getBtn.addEventListener(MouseEvent.CLICK,onGetHandler);
				awardDes.text = "已激活：" + _selectVo.descplay;
				awardDes.textColor = 0x00FF00;
			}else
			{
				finishStatus.gotoAndStop(1);
			}
			_selectItem.setDate(_selectVo);
		}
		/**
		 *返回领取奖励 
		 * @param $obj
		 * 
		 */
		public function getAward($obj:Object):void
		{
			if($obj == null)
			{
				
				return;
			}
			
			var vo:FameHallVo = $obj.vo;
			getBtn.visible = false;
			finishStatus.gotoAndStop(3);
			if(vo == _selectVo)
			{
				switch(vo.status)
				{
					case 0:
						finishStatus.gotoAndStop(1);
						break;
					case 1:
						getBtn.visible = true;
						getBtn.addEventListener(MouseEvent.CLICK,onGetHandler);
						break;
					case 2:
						finishStatus.gotoAndStop(2);
						break;
				}
			}
		}
		/**
		 *领取奖励（激活） 
		 * @param event
		 * 
		 */
		protected function onGetHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			getBtn.removeEventListener(MouseEvent.CLICK,onGetHandler);
			this.dispatchEvent(new FameHallEvent(FameHallEvent.GETAWARD,{honourid:_selectVo.honourid}));
		}
		
		override public function removeEvtListener():void
		{
			btn_1.removeEventListener(MouseEvent.CLICK,onClickBtnHandler);
			btn_2.removeEventListener(MouseEvent.CLICK,onClickBtnHandler);
			btn_3.removeEventListener(MouseEvent.CLICK,onClickBtnHandler);
			getBtn.removeEventListener(MouseEvent.CLICK,onGetHandler);
			super.removeEvtListener();
		}
		override public function destroyView():void
		{
			_scroll.destroy();
			while(_scrollContain.numChildren)
			{
				(_scrollContain.removeChildAt(0) as FameHallItem).destroyView();
			}
			if(GoodsUsePanel.inistance.parent)
				GoodsUsePanel.inistance.parent.removeChild(GoodsUsePanel.inistance);
			super.destroyView();
		}
	}
}